/**
 * 常用JS变量:
 * agentEvent = 代理模式下自动点击模块
 * acEvent= 无障碍模式下自动点击模块
 * device = 设备信息模块
 * file = 文件处理模块
 * http = HTTP网络请求模块
 * shell = shell命令模块
 * thread= 多线程模块
 * image = 图色查找模块
 * utils= 工具类模块
 * global = 全局快捷方式模块
 * 常用java变量：
 *  context : Android的Context对象
 *  javaLoader : java的类加载器对象
 * 导入Java类或者包：
 *  importClass(类名) = 导入java类
 *      例如: importClass(java.io.File) 导入java的 File 类
 *  importPackage(包名) =导入java包名下的所有类
 *      例如: importPackage(java.util) 导入java.util下的类
 *
 */
// 弹窗配置
var height = device.getScreenHeight();
var width = device.getScreenWidth();
let toastExtra={
    "x":width,
    "y":height * 0.33,
    "duration":20000,
    "textColor":"#fff",
    "width":500,
    "height":450,
    "draggable":true
}



//导入依赖函数
let xiaoxin = require("sib/xxAndriod")



function main() {
    //开始再这里编写代码了！！
    let waitTime = readConfigString("cardNo")
    let 功能配置 = readConfigString("sex")
    logd(功能配置)
    waitTime? waitTime = waitTime :waitTime = 1000
    logd(waitTime)
    if (!autoServiceStart(3)) {
        logd("自动化服务启动失败，无法执行脚本")
        exit();
        return;
    }
    logd("开始执行脚本...")

    //当前数量
    let  CurrentAmount = 0
    //当前账号
    let currentAccount = ""
    //任务状态
    let statusmessage = "执行中"
    let filedata =  file.readFile("/sdcard/Download/1.txt");
    if(!filedata){
        toast('数据为空,请检查');
        return
    }
    let MOMOArr = filedata.split(/[(\r\n\s)\r\n\s]+/)
    MOMOArr.forEach((item,index)=>{
        //空值删除
        if (!item) MOMOArr.splice(index, 1);
    })
    MOMOArr = Array.from(new Set(MOMOArr)); // 去重
    MOMOArr.forEach((item,index)=>{
        CurrentAmount += 1
        currentAccount = item
        logd(CurrentAmount,currentAccount);
        sleep(+waitTime)
        utils.openActivity({ "uri":"momochat://immomo.com?goto=[%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99|goto_profile|" + item + "]",});
        toast("任务状态:" + statusmessage + "\n执行进度：" + CurrentAmount + "/" + MOMOArr.length + "\n当前执行账号：" + currentAccount,toastExtra);
        sleep(500)
        if(功能配置 ==="打招呼"){
            logd("打招呼------------------------------")
            xiaoxin.ocrFindWord("打招呼|对话",1000,5000,true)
        }else{
            xiaoxin.xxClick(text,"关注",3000,true)
        }
        sleep(1500)
        utils.openActivity({ "uri":"momochat://immomo.com?goto=[显示文本|goto_homepage|2]",});
        if(index == MOMOArr.length -1) {
            statusmessage = "已完成"
        }
        toast("任务状态:" + statusmessage + "\n执行进度：" + CurrentAmount + "/" + MOMOArr.length + "\n当前执行账号：" + currentAccount,toastExtra);
    })

}



// 关注附近人
function concernNearBy(){

}
function netcardProcessor() {
    logd("开始进行卡密验证")
    // 官方自带的卡密系统
    // appId 和 appSecret的值 请到 http://uc.ieasyclick.com/ 进行注册后提卡
    let appId = "";
    let appSecret = "";
    let cardNo = readConfigString("cardNo")
    if (cardNo == null || cardNo == undefined || cardNo.length <= 0) {
        toast("请输入卡密")
        loge("请输入卡密")
        exit()
        return false;
    }
    let inited = ecNetCard.netCardInit(appId, appSecret)
    if (!inited) {
        toast("初始化卡密失败")
        exit();
        return false;
    }
    let bind = ecNetCard.netCardBind(cardNo)
    let bindResult = false;
    if (bind != null && bind != undefined && bind["code"] == 0) {
        logd("卡密绑定成功")
        let leftDays = bind['data']['leftDays'] + "天";
        logd("剩余时间：" + leftDays);
        logd("激活时间：" + bind['data']['startTime'])
        logd("过期时间：" + bind['data']['expireTime'])
        bindResult = true;
        toast("卡密剩余时间:" + leftDays)
    } else {
        if (bind == null || bind == undefined) {
            loge("卡密绑定失败,无返回值 ")
            let msg = "卡密绑定失败,无返回值"
            loge(msg)
            toast(msg)
        } else {
            let msg = "卡密绑定失败: " + bind["msg"]
            loge(msg)
            toast(msg)
        }
    }
    return bindResult;
}
function autoServiceStart(time) {
    for (var i = 0; i < time; i++) {
        if (isServiceOk()) {
            return true;
        }
        var started = startEnv();
        logd("第" + (i + 1) + "次启动服务结果: " + started);
        if (isServiceOk()) {
            return true;
        }
    }
    return isServiceOk();
}

main();